//
// Created by gogoqiu on 2022/1/12.
//
#include <mysql/mysql.h>
#include <wx/wx.h>

#define MY_SQL_HOST "git.my.host"
#define MY_SQL_USER "gogoqiu"
#define MY_SQL_PASS "512512ZoPoqiu"
#define MY_SQL_DB "repository"


int record() {
    //
    wxString sql;

    const char *_sql;
    char *errmsg;
    int result;
    int nRow, nColumn;
    int i, j;
    int index;
    char **dbResult; //是 char ** 类型，两个*号
    //check first run, null database, null table
    sql.sprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='%s';", "preferences");
    _sql = sql.mb_str(wxConvUTF8);

    MYSQL my_connection;
    int res;
    mysql_init(&my_connection);
    char msg[1024];
    //mysql_real_query
    if (mysql_real_connect(&my_connection, "spsp", "gogoqiu", "P@ssword2", "repository", 0, NULL, 0)) {
        mysql_set_character_set(&my_connection, "utf8");
        //mysql_real_query()
        res = mysql_query(&my_connection, _sql);
        if (!res) {
            return mysql_affected_rows(&my_connection);
        } else {
            sprintf(msg, "mysql select error %d: %s, sql: %s\n", mysql_errno(&my_connection),
                    mysql_error(&my_connection), _sql);
            fprintf(stderr, "%s", msg);
            return -1;
        }
        mysql_close(&my_connection);
    } else {
        fprintf(stderr, "Connection failed\n");
        if (mysql_errno(&my_connection)) {
            sprintf(msg, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
            fprintf(stderr, "%s", msg);
        }
        return -1;
    }
}



